import { BaseWindow } from "../../../component/window/BaseWindow";
import { ObjCreate } from "./ObjCreate";
import { ImagesConst } from "../../resource/ImagesConst";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { ShopItemInfo } from "../../resource/info/ShopItemInfo";
import { ShopConfig } from "../../resource/ShopConfig";
import { ECategory } from "../../../../Message/Public/ECategory";
import { EProp } from "../../../../Message/Public/EProp";
import { ColorInfo } from "../../resource/info/ColorInfo";
import { BaseItem } from "./BaseItem";
import { NumInput } from "./NumInput";
import { GButton } from "../../../../com/mui/controls/GButton";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { Cache } from "../../cache/Cache";
import { MsgManager } from "../../manager/MsgManager";
import { Alert } from "../../../../com/mui/controls/Alert";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { GTextFormat } from "../../../common/GTextFormat";
import { FontUtil } from "../../../common/font/FontUtil";
import { ColorConfig } from "../../resource/ColorConfig";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { ItemData } from "../../resource/info/ItemData";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { GameDefConfig } from "../../resource/GameDefConfig";
import { SPlayerItem } from "../../../../Message/Public/SPlayerItem";
import { Vector } from "../../../../__AS3__/vec/Vector";
type int = number;
//class BuyCoinWindow
    
    export  class BuyCoinWindow extends BaseWindow
    {
        constructor()
        {
            super();
            setSize(300, 235);
            title = "购买铜钱符";
            titleHeight = 26;
            this.init();
            return;
        }

        protected /* override */ createChildren(): void
        {
            super.createChildren();
            return;
        }

        /* internal  */init(): void
        {
            var loc3=null;
            ObjCreate.createTextField("铜钱不够了......", 25, 40, 200, 20, this);
            var loc1=ObjCreate.createTextField("", 0, 40, 280, 20, this);
            loc1.autoSize = flash.text.TextFieldAutoSize.RIGHT;
            loc1.htmlText = "<a href=\'event: 1\'><u><font color=\'#00ff00\'>去市场看看</font></u></a>";
            loc1.addEventListener(flash.events.TextEvent.LINK, this.onLinkTextClickHandler);
            var loc2=this.getCoinItems();
            var loc4=0;
            while (loc4 < loc2.length) 
            {
                if (loc4 != 0) 
                {
                    loc3 = this.createSellItem(loc2[loc4], true);
                }
                else 
                {
                    loc3 = this.createSellItem(loc2[loc4]);
                }
                loc3.x = 20 + 130 * loc4;
                loc3.y = 60;
                this.addChild(loc3);
                ++loc4;
            }
            this._labelBitmap = ObjCreate.createBitmap(ImagesConst.VIPLabel, 170, 85, this);
            return;
        }

        public updateBuyNum(arg1: int=1, arg2: int=1): void
        {
            this._coinInput.currentNum = arg1;
            this._advCoinInput.currentNum = arg2;
            return;
        }

        /* internal  */onLinkTextClickHandler(arg1: Object /* flash.events.TextEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.MarketOpenCoin));
            return;
        }

        /* internal  */getCoinItems(): Vector.<ShopItemInfo>
        {
            var loc1=new Vector.<ShopItemInfo>();
            var loc2=ShopConfig.instance.getShopItemInfoByCode(14015203);
            var loc3=ShopConfig.instance.getItemByType(ECategory._ECategoryProp, EProp._EPropPriceUnitCard, 1, 3, true, -1);
            loc1.push(loc2);
            loc1.push(loc3);
            return loc1;
        }

        /* internal  */createSellItem(arg1: ShopItemInfo, arg2: Boolean=false): Object /* flash.display.Sprite */
        {
            var shopItemInfo: ShopItemInfo;
            var isAdvance: Boolean=false;
            var sp: Object /* flash.display.Sprite */;
            var textFormatCenter: Object /* flash.text.TextFormat */;
            var tf: Object /* flash.text.TextField */;
            var colorInfo: ColorInfo;
            var propBg: Object /* flash.display.Bitmap */;
            var propItem: BaseItem;
            var tfPrice: Object /* flash.text.TextField */;
            var bmpPrice: Object /* flash.display.Bitmap */;
            var numInput: NumInput;
            var btnBuyAndUse: GButton;
            var onBuyAndUse: Function;
            var onClose: Function;

            var loc1;
            sp = null;
            colorInfo = null;
            numInput = null;
            onBuyAndUse = null;
            onClose = null;
            shopItemInfo = arg1;
            isAdvance = arg2;
            onBuyAndUse = (arg1: Object /* flash.events.MouseEvent */): void
            {
                if (ItemsUtil.isVIPItem(shopItemInfo) && !Cache.instance.vip.isVIP) 
                {
                    MsgManager.showRollTipsMsg("您还不是VIP玩家，使用VIP卡即可成为至尊VIP，享受超多优惠。");
                    return;
                }
                Alert.show("您将购买并使用" + HTMLUtil.addColor(numInput.currentNum.toString(), "#ff0000") + "个" + HTMLUtil.addColor(shopItemInfo.itemInfo.item.name, colorInfo.color) + "共消耗" + HTMLUtil.addColor((numInput.currentNum * shopItemInfo.tshopSell.price).toString() + "元宝", "#ffff00") + "。确定要购买吗？", null, Alert.OK | Alert.CANCEL, null, onClose);
                return;
            }
            onClose = (arg1: int): void
            {
                if (arg1 == Alert.OK) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItemAndUse, {"shopItemInfo": shopItemInfo, "amount": numInput.currentNum}));
                    Dispatcher.addEventListener(EventName.ShopMallBuyItemSuccess, onBuySuccess);
                }
                return;
            }
            sp = new Object /* flash.display.Sprite */();
            sp.mouseEnabled = false;
            ObjCreate.createBg(0, 0, 128, 163, sp);
            textFormatCenter = new GTextFormat(FontUtil.songtiName, 12, 11661311);
            textFormatCenter.align = flash.text.TextFormatAlign.CENTER;
            tf = ObjCreate.createTextField("", 32, 6, 65, 20, sp, textFormatCenter);
            colorInfo = ColorConfig.instance.getItemColor(shopItemInfo.itemInfo.item.color);
            tf.htmlText = HTMLUtil.addColor(shopItemInfo.itemInfo.item.name, colorInfo.color);
            propBg = GlobalClass.getBitmap("PropBg");
            propBg.x = 30;
            propBg.y = 22;
            sp.addChild(propBg);
            propItem = new BaseItem();
            propItem.isDragAble = false;
            propItem.label = "";
            propItem.move(35, 27);
            propItem.itemData = new ItemData(shopItemInfo.itemInfo.item.code);
            propItem.drawNow();
            propItem.amount = -1;
            propItem.setSize(60, 60);
            sp.addChild(propItem);
            tfPrice = ObjCreate.createTextField("", 18, 92, 66, 20, sp);
            tfPrice.htmlText = "单价：" + HTMLUtil.addColor(shopItemInfo.tshopSell.price.toString(), GlobalStyle.color6);
            bmpPrice = GlobalClass.getBitmap(GameDefConfig.instance.getEPrictUnitImg(shopItemInfo.unit));
            bmpPrice.x = 80;
            bmpPrice.y = 92;
            sp.addChild(bmpPrice);
            if (isAdvance) 
            {
                numInput = new NumInput();
                numInput.x = 15;
                numInput.y = 110;
                numInput.maxNum = 99;
                numInput.currentNum = 1;
                sp.addChild(numInput);
                this._advCoinInput = numInput;
            }
            else 
            {
                numInput = new NumInput();
                numInput.x = 15;
                numInput.y = 110;
                numInput.maxNum = 99;
                numInput.currentNum = 1;
                sp.addChild(numInput);
                this._coinInput = numInput;
            }
            btnBuyAndUse = ObjCreate.createGButton("购买并使用", 24, 133, 80, 26, sp, "ButtonLarge");
            btnBuyAndUse.addEventListener(flash.events.MouseEvent.CLICK, onBuyAndUse);
            return sp;
        }

        /* internal  */onBuySuccess(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            var loc2=loc1["itemCode"];
            var loc3=loc1["amount"];
            var loc4;
            (loc4 = new SPlayerItem()).itemCode = loc2;
            var loc5=new ItemData(loc4);
            if (ItemsUtil.isCoin(loc5)) 
            {
                this.hide();
                Dispatcher.removeEventListener(EventName.ShopMallBuyItemSuccess, this.onBuySuccess);
            }
            return;
        }

        public static get instance(): BuyCoinWindow
        {
            if (!BuyCoinWindow._instance) 
            {
                BuyCoinWindow._instance = new BuyCoinWindow();
            }
            return BuyCoinWindow._instance;
        }

        /* internal */ /* var */_coinInput: NumInput;

        /* internal */ /* var */_advCoinInput: NumInput;

        /* internal */ /* var */_labelBitmap: Object /* flash.display.Bitmap */;

        /* internal */ static /* var */_instance: BuyCoinWindow;
    }
